package middle;

public class Solution_213 {

    public int rob(int[] nums) {
        if (nums.length < 2) {
            return nums[0];
        }
        int[] array1 = new int[nums.length - 1];
        System.arraycopy(nums, 0, array1, 0, nums.length - 1);
        int[] array2 = new int[nums.length - 1];
        System.arraycopy(nums, 1, array2, 0, nums.length - 1);
        return Math.max(robSub(array1), robSub(array2));
    }

    public int robSub(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        } else if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }
        int[] array = new int[nums.length];
        array[0] = nums[0];
        array[1] = Math.max(nums[0], nums[1]);
        for (int i = 2; i < nums.length; i++) {
            array[i] = Math.max(array[i - 2] + nums[i], array[i - 1]);
        }
        return array[array.length - 1];
    }

    /*public int rob(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        return Math.max(robSub(nums, 0, nums.length - 2), robSub(nums, 1, nums.length - 1));
    }

    public int robSub(int[] nums, int start, int end) {
        int length = end - start + 1;
        if (length == 1) {
            return nums[start];
        } else if (length == 2) {
            return Math.max(nums[start], nums[end]);
        }
        int[] array = new int[length];
        array[0] = nums[start];
        array[1] = Math.max(nums[start], nums[start + 1]);
        for (int i = 2; i < length; i++) {
            array[i] = Math.max(array[i - 2] + nums[i + start], array[i - 1]);
        }
        return array[array.length - 1];
    }*/

    public static void main(String[] args) {
        Solution_213 model = new Solution_213();
        System.out.println(model.rob(new int[]{0}));
        System.out.println(model.rob(new int[]{2, 3, 2}));
        System.out.println(model.rob(new int[]{1, 2, 3, 1}));
        System.out.println(model.rob(new int[]{1, 2, 3}));
    }
}
